﻿@{
    ViewBag.Title = "RuleForm";
    Layout = "~/Views/Shared/_FlowForm.cshtml";
}
<!--jqgrid表格组件start-->
<link href="~/Content/scripts/plugins/jqgrid/jqgrid.css" rel="stylesheet" />
<script src="~/Content/scripts/plugins/jqgrid/grid.locale-cn.js"></script>
<script src="~/Content/scripts/plugins/jqgrid/jqgrid.min.js"></script>
<!--表格组件end-->

<script>
    var keyValue = request('keyValue');
    var ICode = request('ICode');
    var _CountType = "0";
    $(function () {
        initControl();
        GetGrid();
    })
    function initControl() {
        //类型方式
        $('input:radio[name="Type"]').change(function (e) {
            _CountType = e.currentTarget.value;
            if (_CountType == "0")
            { $("#CountFormula").removeAttr("hidden").attr("class", "form-control").parent().prev().text("指标计算公式"); $("#CountProcedure").attr("hidden", "hidden"); }
            else
            { $("#CountProcedure").removeAttr("hidden").parent().prev().text("指标存储过程"); $("#CountFormula").attr("hidden", "hidden").removeAttr("class"); }
        });
        //项目类型
        $("#ICode").ComboBoxTree({
            url: "../../PropertyManage/CS_Polling/GetItemTreeJson",
            description: "==请选择==",
            height: "200px"
        });
        $("#ICode").ComboBoxTreeSetValue(ICode);

        //存储过程
        $("#CountProcedure").ComboBoxTree({
            description: "==请选择==",
            height: "200px"
        });
    }
    //选取图标
    function SelectIcon() {
        dialogOpen({
            id: "SelectIcon",
            title: '选取图标',
            url: '/SystemManage/BussinessIndicator/Icon?ControlId=Icon',
            width: "1000px",
            height: "600px",
            btn: false
        })
    }

    function GetGrid() {
        var $gridTable = $("#gridTable1");
        $gridTable.jqGrid({
            datatype: "json",
            height: 189,
            autowidth: true,
            unwritten: false,
            colModel: [
                { label: "Id", name: "Id", hidden: true },
                 { label: "Flag", name: "Flag", hidden: true },
                { label: "界限值说明", name: "ValueMemo", index: "Sort", align: "center", sortable: false },
                { label: "界限值1", name: "MinValue", width: 60, align: "center", sortable: false },
                { label: "界限值2", name: "MaxValue", width: 100, align: "center", sortable: false },
                {
                    label: "颜色", name: "Color", width: 100, align: "center", sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        return "<div style='width:70px;background-color:" + cellvalue + "'>&nbsp;</div>";
                    }
                }
            ],
            caption: "规则设计",
            rowNum: "1000",
            rownumbers: true,
            shrinkToFit: false,
            gridview: true,
            hidegrid: false
        });
        //获取表单
        if (!!keyValue) {
            $.SetForm({
                url: "/SystemManage/BussinessIndicator/GetRuleEntity",
                param: { keyValue: keyValue },
                success: function (data) {
                    $("#form1").SetWebControls(data);
                    $("#ICode").ComboBoxSetValue(data.ICode);
                    $("#Type").ComboBoxSetValue(data.CountType);
                    $gridTable[0].addJSONData(JSON.parse(data.ValueFlag));
                }
            });
        }
    }

    //添加
    function btn_add_field() {
        dialogOpen({
            id: "ValueForm",
            title: '添加',
            url: '/SystemManage/BussinessIndicator/ValueForm',
            width: "400px",
            height: "330px",
            callBack: function (iframeId) {
                top.frames[iframeId].AcceptClick();
            }
        });
    };
    //修改
    function btn_edit_field() {
        var keyValue = $("#gridTable1").getGridParam('selrow');
        if (checkedRow(keyValue)) {
            dialogOpen({
                id: "ValueForm",
                title: '修改',
                url: '/SystemManage/BussinessIndicator/ValueForm?keyValue=' + keyValue,
                width: "400px",
                height: "330px",
                callBack: function (iframeId) {
                    top.frames[iframeId].AcceptClick();
                }
            });
        }
    }
    //移除
    function btn_delete_field() {
        var keyValue = $("#gridTable1").getGridParam('selrow');
        if (keyValue) {
            dialogConfirm('注：您确定要移除吗？该操作将无法恢复？', function (r) {
                if (r) {
                    $("#gridTable1").delRowData(keyValue);
                    dialogMsg("移除成功。", 1);
                }
            });

        } else {
            dialogMsg('请选择需要移除的数据！', 0);
        }
    }
    //保存表单
    function AcceptClick() {
        if (!$('#form1').Validform()) {
            return false;
        }
        var obj = $("#gridTable1").jqGrid("getRowData");
        if (obj.length <= 0) {
            dialogMsg("至少需要一种规则。", 7);
            return false;
        }
        var postData = $("#form1").GetWebControls(keyValue);
        postData["ICode"] = $("#ICode").attr('data-value');
        postData["CountType"] = _CountType;
        for (var i = 0; i < obj.length; i++) {
            obj[i].Color = obj[i].Color.substring(40, 47);
        }
        postData["ValueFlag"] = JSON.stringify(obj);
        $.SaveForm({
            url: "/SystemManage/BussinessIndicator/SaveRuleForm?keyValue=" + keyValue,
            param: postData,
            loading: "正在保存数据...",
            success: function () {
                top.Form.$("#gridTable").resetSelection();
                top.Form.$("#gridTable").trigger("reloadGrid");
            }
        })
    }
</script>
<div class="border-left border-right border-top" style="margin: 5px; margin-bottom: 0px; padding-bottom: 2px;">
    <div style="height: 170px;">
        <table class="form">
            <tr>
                <th class="formTitle">指标名称<font face="宋体">*</font></th>
                <td class="formValue">
                    <input id="Name" type="text" class="form-control" placeholder="请输入名称" onblur="$.ExistField(this.id, '../../SystemManage/BussinessIndicator/ExistRuleCode')" isvalid="yes" checkexpession="NotNull" />
                </td>
                <th class="formTitle">指标编码<font face="宋体">*</font></th>
                <td class="formValue">
                    <input id="Code" type="text" placeholder="请输入编码" onblur="$.ExistField(this.id, '../../SystemManage/BussinessIndicator/ExistRuleName')" class="form-control" isvalid="yes" checkexpession="NotNull" />
                </td>
            </tr>
            <tr>
                <th class="formTitle">计算类型<font face="宋体">*</font></th>
                <td class="formValue">
                    <input type="radio" value="0" name="Type" checked="checked" />计算公式
                    <input type="radio" value="1" name="Type" />存储过程
                </td>
                <th class="formTitle">指标计算公式</th>
                <td class="formValue">
                    <input id="CountFormula" type="text" class="form-control" placeholder="请输入计算公式" />
                    <div id="CountProcedure" type="selectTree" hidden="hidden" class="ui-select"></div>
                </td>
            </tr>
            <tr>
                <th class="formTitle">选项</th>
                <td class="formValue" colspan="3">
                    <input type="checkbox" id="AllowShow" />是否在首页显示
                    <input type="checkbox" id="EnabledMark" />有效
                </td>
            </tr>
            <tr>
                <th class="formTitle">指标排序<font face="宋体">*</font></th>
                <td class="formValue ">
                    <input id="Sort" type="text" class="form-control" isvalid="yes" checkexpession="NotNull" />
                </td>
                <th class="formTitle">显示图标</th>
                <td class="formValue">
                    <input id="Icon" type="text" class="form-control" />
                    <span class="input-button" onclick="SelectIcon()" title="选取图标">...</span>
                </td>
            </tr>
        </table>
    </div>
</div>
<div style="margin: 5px; margin-top: 0px; margin-bottom: 0px;">
    <table id="gridTable1"></table>
    <style>
        .ui-jqgrid .ui-jqgrid-caption {
            text-align: right;
            padding-right: 5px;
            background-color: #F5F5F5;
        }
    </style>
</div>
<div class="toolbar" style="position: absolute; top: 185px; left: 10px;">
    <a id="lr-addIndex" class="btn btn-success btn-xs" onclick="btn_add_field()"><i class="fa fa-plus"></i>&nbsp;添加</a>
    <a id="lr-editIndex" class="btn btn-info btn-xs" onclick="btn_edit_field()"><i class="fa fa-pencil-square-o"></i>&nbsp;修改</a>
    <a id="lr-deleteIndex" class="btn btn-danger btn-xs" onclick="btn_delete_field()"><i class="fa fa-trash-o"></i>&nbsp;移除</a>
</div>
<script>$('.toolbar').authorizeButton()</script>
